package com.kgc.springboot.hangduo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.springboot.hangduo.mapper.CatalogMapper;
import com.kgc.springboot.hangduo.pojo.Catalog;
import com.kgc.springboot.hangduo.pojo.CatalogExample;
import com.kgc.springboot.hangduo.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: ASUS
 * @date: 2020/12/27 14:32
 * @description:
 */
@Service
public class CatalogServiceImpl implements CatalogService {
    @Autowired
    private CatalogMapper catalogMapper;
    @Override
    public PageInfo<Catalog> selectAll(Integer lid) {

        PageHelper.orderBy("sequence");
        CatalogExample catalogExample = new CatalogExample();
        if(lid.equals(0)){
            catalogExample.createCriteria().andSuperiorIsNull();
        }else {
            catalogExample.createCriteria().andSuperiorIsNull().andLawsIdEqualTo(lid);
        }
        List<Catalog> catalogs = catalogMapper.selectByExample(catalogExample);
        PageInfo<Catalog> pageInfo = new PageInfo<>(catalogs);
        return pageInfo;
    }

    @Override
    public List<Catalog> selectByLawId(Integer lid) {

        CatalogExample example = new CatalogExample();
        if (lid != null){
            example.createCriteria().andLawsIdEqualTo(lid);
        }

        List<Catalog> catalogs = catalogMapper.selectByExample(example);

        if (catalogs!=null && catalogs.size()>0){
            return catalogs;
        }

        return null;
    }

    public List<Catalog> searchAll() {
        List<Catalog> catalogs = catalogMapper.selectByExample(null);
        return catalogs;
    }

    @Override
    public int addCatalog(Catalog catalog) {
        return catalogMapper.insertSelective(catalog);
    }

    @Override
    public int delCatalog(int id) {
        return catalogMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Catalog selectById(Integer id) {
        return catalogMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateCatalogById(Catalog catalog) {
        return catalogMapper.updateByPrimaryKeySelective(catalog);
    }


}
